package com.zzyl.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.dto.DevicePageQueryDto;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zzyl.base.ResponseResult.success;

@RestController
@RequestMapping("/iot")
@Api(tags = "管理端 - 物联网iot相关接口")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public ResponseResult<Void> syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public ResponseResult<List<DeviceVo>> getAllProduct(){
        List<DeviceVo> list = deviceService.getAllProduct();
        return success(list);
    }

    @PostMapping("/RegisterDevice")
    @ApiOperation(value = "注册设备")
    public ResponseResult registerDevice(@RequestBody DeviceDto deviceDto){
        deviceService.registerDevice(deviceDto);
        return success();
    }

    @GetMapping("/pageQueryDevice")
    @ApiOperation(value = "分页查询设备列表")
    public ResponseResult<PageResponse<DeviceVo>> selectDeviceByPage(DevicePageQueryDto devicePageQueryDto){
        PageResponse<DeviceVo> pageResponse = deviceService.selectDeviceByPage(devicePageQueryDto);
        return success(pageResponse);
    }


    @PostMapping("/QueryDeviceDetail")
    @ApiOperation(value = "查询设备详细数据")
    @ApiOperationSupport(includeParameters = {"deviceDto.iotId","deviceDto.productKey"})
    public ResponseResult<DeviceVo> selectDeviceDetail(@RequestBody DeviceDto deviceDto){
        DeviceVo deviceVo = deviceService.selectDeviceDetail(deviceDto);
        return success(deviceVo);
    }

    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation(value = "查询指定设备的物模型运行状态")
    @ApiOperationSupport(includeParameters = {"deviceDto.deviceName","deviceDto.productKey"})
    public ResponseResult queryDevicePropertyStatus(@RequestBody DeviceDto deviceDto){
        Object data = deviceService.queryDevicePropertyStatus(deviceDto);
        return success(data);
    }

    @PostMapping("/QueryThingModelPublished")
    @ApiOperation(value = "查看指定产品的已发布物模型中的功能定义详情")
    @ApiOperationSupport(includeParameters = {"deviceDto.productKey"})
    public ResponseResult queryThingModelPublished(@RequestBody DeviceDto deviceDto){
        Object data = deviceService.queryThingModelPublished(deviceDto);
        return success(data);
    }

    @PostMapping("/UpdateDevice")
    @ApiOperation(value = "修改设备备注名称")
    public ResponseResult updateDevice(@RequestBody DeviceDto deviceDto){
        deviceService.updateDevice(deviceDto);
        return success();
    }

    @DeleteMapping("/DeleteDevice")
    @ApiOperation(value = "删除设备")
    @ApiOperationSupport(includeParameters = {"deviceDto.productKey","deviceDto.iotId"})
    public ResponseResult deleteDevice(@RequestBody DeviceDto deviceDto){
        deviceService.deleteDevice(deviceDto);
        return success();
    }


    @GetMapping("/pageQueryDeviceServiceData")
    @ApiOperation(value = "分页查询设备服务调用数据")
    public ResponseResult pageQueryDeviceServiceData(Long endTime,String iotId,
                                                     Integer pageSize,Long startTime){
        Object data = deviceService.pageQueryDeviceServiceData(endTime,iotId,pageSize,startTime);
        return success(data);
    }

}